//
//  NShenbaoTypeDetailViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/22.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NShenbaoTypeDetailViewController.h"
#import "NShenbaoTypeModel.h"
#import "NZhaopinCell.h"
#import "NMiaoshuCell.h"
#import "NFuJianCell.h"
#import "NFujianModel.h"
//#import "NFuJianCell~.h"
@interface NShenbaoTypeDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)NSArray *infoArray;

///附件
@property(nonatomic,strong)NSArray *fujianArray;
@property(nonatomic,strong)NSArray *fujianInfoArray;

//所属行业
@property (nonatomic,copy)NSString *industry;

//备注
@property (nonatomic,copy)NSString *infoDetail;

@end

@implementation NShenbaoTypeDetailViewController

//求职详情
#define cellIdentifier @"zhaopin"
#define miaoshuCell @"miaoshu"
#define fujianCell @"fujian"
#define SectionHeaderHeight  10.f

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //        _tableView.rowHeight = 115.f;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NZhaopinCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        [_tableView registerNib:[UINib nibWithNibName:@"NMiaoshuCell" bundle:nil] forCellReuseIdentifier:miaoshuCell];
         [_tableView registerNib:[UINib nibWithNibName:@"NFuJianCell" bundle:nil] forCellReuseIdentifier:fujianCell];
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}

//申报类型详情
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"项目申报详情" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.dataArray = [NSMutableArray array];
    
    self.titleArray = @[@"申报名称",@"申报科室",@"申报级别",@"开始时间",@"结束时间",@"流程类型",@"申报模板",];
//    self.infoArray = @[@"国家高新企业",@"综合科",@"国家级",@"1990-9-8",@"1990-10-8",@"通用项目申报流程",@"普通类型",];
    self.fujianArray = @[@"项目申报附件规则表",@"附件名称",@"附件要求",@"是否必须",@"附件备注",];
    self.fujianInfoArray = @[@"",@"企业标识截图",@"不大于1M的图片",@"是",@"无",];
    [self.view addSubview:self.tableView];
    
    [self loadData];
    [self loadFujianRuleListData];
    // Do any additional setup after loading the view from its nib.
}

- (void)loadData{
    
    NSDictionary *dic = @{@"projectDeclarationId":self.shenbaoId?self.shenbaoId:@""};
    //
    [[NNRCNetRequest manager]getWithUrl:kShenbaoTypeDetailUrl params:dic success:^(id result) {
        
        YYLog(@"%@",result);
//        WeakStament(ws);
        if ([[result objectForKey:@"code"] integerValue] == 0) {
            NSDictionary *dic = result[@"page"];
//            NSArray *valueArray = [dic allValues];
//            YYLog(@"%@",valueArray);
            NShenbaoTypeModel *model = [NShenbaoTypeModel mj_objectWithKeyValues:dic];
            self.industry = model.projectDeclarationIndustry;
//            [self.dataArray addObject:model];
            [self configCellWithModel:model];
            [self.tableView reloadData];
        }
    } failure:^(id failure) {
         YYLog(@"%@",failure);
        
    }];
}


///附件规则表数据  和这个不同的是 我的申报-详情中自带了附件表    而这个附件规则表是一个单独的接口

- (void)loadFujianRuleListData{
    
    NSDictionary *dic = @{@"projectDeclarationId":self.shenbaoId?self.shenbaoId:@""};
    //
    [[NNRCNetRequest manager]getWithUrl:kShenbaoTypeDetailFuJianRuleListUrl params:dic success:^(id result) {
        
        YYLog(@"%@",result);
        WeakStament(ws);
        if ([[result objectForKey:@"code"] integerValue] == 0) {
            
            NSArray *arr = result[@"page"][@"records"];
            YYLog(@"%@",arr);
            for (NSDictionary *dict in arr) {
                NFujianModel *model = [NFujianModel mj_objectWithKeyValues:dict];
                [ws.dataArray addObject:model];
            }
            [ws.tableView reloadData];
            
        }else{
            [AFToast showText:result[@"msg"]];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
        
    }];
}

- (void)configCellWithModel:(NShenbaoTypeModel *)model{
    
    NSString *name = model.projectDeclarationTitle;
    NSString *keshi = model.projectDeclarationDept;
    NSString *jibie = model.projectDeclarationLevel;
    NSString *startTime = [[ToolKit getTimeFromTimesTamp:model.projectDeclarationStartTime] substringToIndex:10];;
    NSString *endTime = [[ToolKit getTimeFromTimesTamp:model.projectDeclarationEndTime] substringToIndex:10];
    NSString *proType = model.projectDeclarationProcessType;
    NSString *module = model.projectDeclarationModule;
    self.infoArray = @[name,keshi,jibie,startTime,endTime,proType,module];
    
    self.infoDetail = model.projectDeclarationBz;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        return 45.f;
        
    }else if (indexPath.section==1){
        
//        CGFloat adjustValue = 90.f;//这个值在不同的屏幕上显示会有差别,尽量给的大一点，他包括内容label的上下的margin 或者理解成padding
//        return  [self getCellHeight:self.industry withWidth:ScreenWidth-30]+adjustValue;
        
        return 56.f;
        
    }
    else if (indexPath.section==2){
        return 155.f;

    }else{
        //indexPath.section==3
        return 180.f;
    }
    
    return 0.f;
    
}
#pragma mark - datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 4;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==1||section==2) {
        return 1;
    }else if (section==0){
        return self.titleArray.count;
    }else if (section==3){
        //附件有单独的接口了，需要单独处理
        return self.dataArray.count;
        
//        return 2;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //     NShenbaoTypeModel *model = [self.dataArray lastObject];
    if (indexPath.section==0) {
        NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        cell.area.hidden = YES;
        cell.date.hidden = YES;
        cell.company.hidden = YES;
        cell.salary.hidden = false;
        cell.name.text = self.titleArray[indexPath.row];
        cell.salary.text = self.infoArray[indexPath.row];
//        [self configCell:cell withModel:model];
        cell.nameLeftMargin.constant = 25.f;
        return cell;
    }
    else if(indexPath.section==1){
        NZhaopinCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        cell.nameLeftMargin.constant = 25.f;
        cell.area.hidden = YES;
        cell.date.hidden = YES;
        cell.company.hidden = YES;
        cell.salary.hidden = false;
        cell.name.text = @"所属行业";
        cell.salary.text = self.industry;
//         cell.salary.text = @"附件名称不超过20个是字符附件名称不超过20个是字符";
        return cell;
    }
    else if (indexPath.section==2){
        NMiaoshuCell *cell = [tableView dequeueReusableCellWithIdentifier:miaoshuCell forIndexPath:indexPath];
        cell.nameLabel.text = @"备注";
        cell.detailLable.text = self.infoDetail;
        return cell;
    }
    else if (indexPath.section==3){
        NFujianModel *model = self.dataArray[indexPath.row];
        NFuJianCell *cell = [tableView dequeueReusableCellWithIdentifier:fujianCell forIndexPath:indexPath];
        cell.fujianView.hidden = YES;
        cell.model = model;
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 179, ScreenWidth, 0.5)];
        line.backgroundColor = kLightGrayColor;
        [cell.contentView addSubview:line];
        
        return cell;

    }
    return nil;
}


#pragma mark - 动态行高
///推荐方法 直接返回行高
- (CGFloat)getCellHeight:(NSString *)str withWidth:(CGFloat)width{
    CGSize size = CGSizeMake(width,MAXFLOAT); //设置一个行高上限
    NSDictionary *attribute = @{NSFontAttributeName: kFont13};//给一个字体大小
    CGSize labelsize = [str boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine| NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return ceil(labelsize.height);
}

//根据字符串内容返回字符串的size
- (CGSize)sizeWithLabelWithContent:(NSString *)content width:(CGFloat)width font:(UIFont *)font{
    NSDictionary *dict=@{NSFontAttributeName : font};
    CGRect rect=[content boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dict context:nil];
    CGFloat sizeWidth=ceilf(CGRectGetWidth(rect));
    CGFloat sizeHieght=ceilf(CGRectGetHeight(rect));
    return CGSizeMake(sizeWidth, sizeHieght);
}

#pragma mark - section head
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==3) {
        return 80.f;
    }else{
         return SectionHeaderHeight;
    }
   //如果设置section的header高度，不设置footer高度，footer默认等于header高度
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section==3) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 80.f)];
        view.backgroundColor = kBackgroundColor;
        
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 13, ScreenWidth, 60.f)];;
        whiteView.backgroundColor = kWhiteColor;
        
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 200, 30)];
        label.text = @"项目申报附件规则表";
        label.textColor = kDarkGrayColor;
        label.font = kFont15;
        [view addSubview:whiteView];
        [view addSubview:label];
        
        return view;
        
    }else{
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
        view.backgroundColor = kBackgroundColor;
        return view;
    }
    
}

#pragma mark - section foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}



@end
